﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Text;
using System.Data.SqlClient;

namespace CoreSystem
{
    public class SysMenu
    {
        SortedList<string, SysBaseMenuItem> ListOfMenuItem;
        private MenuStrip _menu;
        private string Connstr;
        public MenuStrip MainMenu
        {
            get { return _menu; }
        }
        public SysMenu(string ConnectionString)
        {
            this.Connstr = ConnectionString;
            _menu = new  MenuStrip();
            _menu.Name = "MainMenu";            
        }
        public void AddEvent(string name_menu, EventHandler _event)
        {
            SysBaseMenuItem _baseitem;
            ListOfMenuItem.TryGetValue(name_menu, out _baseitem);
	    if (_baseitem == null) return;
            ToolStripMenuItem _subitem = _baseitem.MenuDropDownItem as ToolStripMenuItem; ;
            if(_subitem!=null) _subitem.Click += _event;
        }
        public void CreateMenu(string tblsysmenu)
        {            
            SqlConnection connection= null;
            SqlCommand command = null;
            System.Data.IDataReader i_reader;
            try
            {
                string select_str = "SELECT * FROM " + tblsysmenu+ " ORDER BY level_menu,order_menu, parents";
                connection =new  SqlConnection(Connstr);
                command =new  SqlCommand(select_str, connection);

                //System.Data.DataTable dt_sysmenu = new System.Data.DataTable();
                //SqlDataAdapter adapter = new SqlDataAdapter(command);
                //adapter.Fill(dt_sysmenu);
                connection.Open();
                i_reader = command.ExecuteReader() ;//dt_sysmenu.CreateDataReader();
                _menu.Items.Clear();

                ListOfMenuItem = new SortedList<string, SysBaseMenuItem>(i_reader.Depth);
                SysBaseMenuItem _baseitem, parents;
                SysMenuItemEntity _itemparents, sub_toolstrip=null;
                SysMenuSeparator sub_separator=null;
                
                while (i_reader.Read())
                {
                    // Nếu kiểu separator
                    if (i_reader["caption"].ToString() == "-")                    
                        _baseitem  = new SysMenuSeparator();                                           
                    else                
                        _baseitem = new SysMenuItemEntity();

                    sub_separator = _baseitem as SysMenuSeparator;
                    sub_toolstrip = _baseitem as SysMenuItemEntity;
                

                    _baseitem.Caption = i_reader["caption"].ToString();
                    _baseitem.Name_Menu = i_reader["name_menu"].ToString();
                    _baseitem.Parents = i_reader["parents"].ToString();
                    _baseitem.Enable = bool.Parse(i_reader["enable"].ToString());
                    _baseitem.Visible = bool.Parse(i_reader["visible"].ToString());
                    _baseitem.Short_Cut = i_reader["short_cut"].ToString();

                    if (_baseitem.Parents.Trim() == "")
                    {
                        _itemparents = (SysMenuItemEntity)_baseitem;
                        if (_itemparents != null) _menu.Items.Add(_itemparents.SysMenuItem);                        
                    }
                    else
                    {
                        ListOfMenuItem.TryGetValue(_baseitem.Parents, out parents);
                        _itemparents = parents as SysMenuItemEntity;
                        if (_itemparents != null)
                        {
                            if (sub_separator != null)
                                _itemparents.SysMenuItem.DropDownItems.Add(sub_separator.SysMenuItem);
                            else
                                _itemparents.SysMenuItem.DropDownItems.Add(sub_toolstrip.SysMenuItem);
                        }
                    }
                    // Thêm vào list
                    if (_baseitem.Caption !="-")
                        ListOfMenuItem.Add(_baseitem.Name_Menu, _baseitem);                   
                }
                if (!i_reader.IsClosed) i_reader.Close();
                //dt_sysmenu.Dispose();
            }
            catch (Exception ex) { MessageBox.Show(ex.Message.ToString()); }
            finally
            {
                if(command!=null) command.Dispose();
                if(connection!= null)connection.Close();               
            }
        }
    }
}
